package com.yonyou.ucf.mdf.app.isvdxq.secondpart.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.entity.VideologEntity;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.VideologMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.utils.MyTool;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideologService {
    @Autowired
    private VideologMapperImpl videologMapper;

    public VideologEntity SelectById(String id){
        return videologMapper.selectById(id);
    }
    public List<VideologEntity> SelectByIds(List<String> ids){
        return videologMapper.selectByIds(ids);
    }
    public List<VideologEntity> SelectAll(String tenantId){
        return videologMapper.selectAll(tenantId);
    }
    public Integer InsertEntity(VideologEntity entity) {
        return videologMapper.insert(entity);
    }

    public Integer UpdateEntity(VideologEntity entity) {
        return videologMapper.updateByPrimaryKey(entity);
    }

    public Integer DeleteById(Long id) {
        return videologMapper.deleteByPrimaryKey(id);
    }

    public Integer DeleteByIds(List<Long> ids) {
        return videologMapper.deleteByPrimaryKeys(ids);
    }

    //新增并更新
    public Integer InsertBatch(List<VideologEntity> list) {
        if (list.size() == 0) return 0;
        long start = System.currentTimeMillis();
        int res = videologMapper.insertBatch(list);
        long end = System.currentTimeMillis();
        System.out.println("---------------" + (start - end) + "---------------");
        return res;
    }
    /**
     * 批量上传 name,tenantId,orgId,creator,creatorName,type
     * */
    public boolean AddEntitys(String dataListStr) throws IOException {
        List<VideologEntity> dataList = new ArrayList<>();
        dataList = RequestTool.getBean(dataListStr, new TypeReference<List<VideologEntity>>() {
        });
        if (dataList == null || dataList.size() == 0) {return false;}

        String dateString = MyTool.GenerateTime("yyyy-MM-dd HH:mm:ss");
        for (VideologEntity item : dataList) {
            item.setId(Long.parseLong(MyTool.getRandomId()));
            item.setCreate_time(dateString);
            item.setStatus(1);
            item.setVersion(2);
        }
        int i = InsertBatch(dataList);
        return i != 0;
    }
}
